<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="cabinetFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="柜体名称" prop="deviceName">
        <el-input v-model="drawerProps.row!.deviceName" placeholder="请填写柜体名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="资产编号" prop="deviceNo">
        <el-input v-model="drawerProps.row!.deviceNo" placeholder="请填写资产编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="装置类型" prop="deviceType">
        <el-select v-model="drawerProps.row!.deviceType" placeholder="请选择装置类型" clearable>
          <el-option v-for="item in deviceType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="面数" prop="facesNums">
        <el-select v-model="drawerProps.row!.facesNums" placeholder="请选择面数" clearable>
          <el-option v-for="item in facesNums" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="通讯ip地址" prop="deviceIp">
        <el-input v-model="drawerProps.row!.deviceIp" placeholder="请填写通讯ip地址" clearable></el-input>
      </el-form-item>

      <el-form-item label="通讯端口号" prop="devicePort">
        <el-input v-model="drawerProps.row!.devicePort" placeholder="请填写通讯端口号" clearable></el-input>
      </el-form-item>
      <el-form-item label="装置地址" prop="deviceAddress">
        <el-input v-model="drawerProps.row!.deviceAddress" placeholder="请填写装置地址（主板地址）" clearable></el-input>
      </el-form-item>
      <el-form-item label="生产厂家" prop="deviceFactory">
        <el-input v-model="drawerProps.row!.deviceFactory" placeholder="请填写生产厂家" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="CabineDrawer">
import { ref, reactive } from "vue";
import { deviceType, facesNums } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { Cabinet } from "@/api/interface";
const validateNumber = (rule: any, value: string, callback: (arg0?: Error | undefined) => void) => {
  if (!/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
    callback(new Error("请输入数字值"));
  } else {
    callback();
  }
};

const rules = reactive({
  deviceName: [{ required: true, trigger: "blur", message: "请填写柜体名称" }],
  deviceNo: [{ required: true, trigger: "blur", message: "请填写资产编号" }],
  deviceType: [{ required: true, trigger: "blur", message: "请选择装置类型" }],
  facesNums: [{ required: true, trigger: "blur", message: "请选择面数" }],
  deviceIp: [
    { required: true, trigger: "blur", message: "请填写通讯ip地址" },
    {
      required: true,
      trigger: "blur",
      pattern: /([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$/,
      message: "通讯ip地址格式不正确"
    }
  ],
  devicePort: [{ required: true, trigger: "blur", message: "请填写通讯端口号" }],
  deviceAddress: [
    { required: true, trigger: "blur", message: "请填写装置地址（主板地址）" },
    { validator: validateNumber, trigger: "blur", message: "装置地址（主板地址）必须由数字组成" }
  ],
  deviceFactory: [{ required: true, trigger: "blur", message: "请填写生产厂家" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Cabinet.ResCabinetInfoList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const cabinetFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  cabinetFormRef.value && cabinetFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  cabinetFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!({ ...drawerProps.value.row });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
defineExpose({
  acceptParams
});
</script>
